'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: upload_file_schema.py
* @Time: 2025/9/25
* @All Rights Reserve By Brtc
'''
from flask_wtf import FlaskForm
from flask_wtf.file import FileSize, FileRequired, FileAllowed
from marshmallow import Schema, fields, pre_dump
from wtforms import FileField

from internal.entity.upload_file_entity import ALLOWED_DOCUMENT_EXTENSION, ALLOWED_IMAGE_EXTENSION
from internal.model import UploadFile


class UploadReq(FlaskForm):
    """上传文件请求校验"""
    file = FileField('file', validators=[
        FileRequired("上传文件不能为空"),
        FileAllowed(ALLOWED_DOCUMENT_EXTENSION, message=f"仅允许上传{'/'.join(ALLOWED_DOCUMENT_EXTENSION)}格式的文件"),
        FileSize(max_size=16*1024*1024, message="上传文件最大不能超过16M")
    ])

class UploadFileResp(Schema):
    """上传文件返回接口数据"""
    id = fields.UUID(dump_default="")
    account_id = fields.UUID(dump_default="")
    name = fields.String(dump_default="")
    key = fields.String(dump_default="")
    size = fields.Integer(dump_default=0)
    extension = fields.String(dump_default="")
    mime_type = fields.String(dump_default="")
    created_at = fields.Integer(dump_default=0)

    @pre_dump
    def process_data(self, data:UploadFile, **kwargs):
        return {
            "id":data.id,
            "account_id":data.account_id,
            "name":data.name,
            "key":data.key,
            "size":data.size,
            "extension":data.extension,
            "mime_type":data.mime_type,
            "created_at":int(data.created_at.timestamp())
        }

class UploadImageReq(FlaskForm):
    """上传图片请求结构体"""
    file = FileField('file', validators=[
        FileRequired("上传图片不能为空"),
        FileAllowed(ALLOWED_IMAGE_EXTENSION, message=f"仅允许上传{'/'.join(ALLOWED_IMAGE_EXTENSION)}格式的文件"),
        FileSize(max_size=16 * 1024 * 1024, message="上传图片最大不能超过16M")
    ])


